package cn.edu.zafu.bigdata.x.zhaobolei.Sixth;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;

public class ZZFileAppend {

    public static void appendToFile(String fileName, String content) {
        try {
            FileWriter fileWriter = new FileWriter(fileName, true);
            BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
            bufferedWriter.write(content);
            bufferedWriter.newLine();
            bufferedWriter.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void readFile(String fileName) {
        try {
            File file = new File(fileName);
            if (file.exists()) {
                FileReader fileReader = new FileReader(file);
                BufferedReader bufferedReader = new BufferedReader(fileReader);
                String line;
                while ((line = bufferedReader.readLine())!= null) {
                    System.out.println(line);
                }
                bufferedReader.close();
            } else {
                System.out.println("文件不存在！");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.println("请输入要追加到文件的文本：");
        String inputText = scanner.nextLine();

        String fileName = "files/"+"202211080221.txt";

        appendToFile(fileName, inputText);
        readFile(fileName);

        scanner.close();
    }
}
